package com.haina.vote.mapper;
import com.haina.vote.entity.KqFwModel;
import com.haina.vote.entity.KqFwPerformance;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface KqFwMapper {
    @Select("select * from tb_kqfw_task ORDER BY create_time DESC")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "number",column = "number"),
            @Result(property = "organization",column = "organization"),
            @Result(property = "taskname",column = "taskname"),
            @Result(property = "linkman",column = "linkman"),
            @Result(property = "telephone",column = "telephone"),
            @Result(property = "givetime",column = "givetime"),
            @Result(property = "timelimit",column = "timelimit"),
            @Result(property = "tasktype",column = "tasktype"),
            @Result(property = "taskbackground",column = "taskbackground"),
            @Result(property = "taskmark",column = "taskman"),
            @Result(property = "specification",column = "specification"),
            @Result(property = "keywords",column = "keywords"),
            @Result(property = "taskfeedback",column = "taskfeedback"),
            @Result(property = "kqfwPerformance",column = "id",javaType = List.class,many = @Many(select = "com.haina.vote.mapper.KqFwMapper.findPerformanceById"))
    })
    public List<KqFwModel> findTask();

    @Select("select * from tb_kqfw_task where id=#{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "number",column = "number"),
            @Result(property = "organization",column = "organization"),
            @Result(property = "taskname",column = "taskname"),
            @Result(property = "linkman",column = "linkman"),
            @Result(property = "telephone",column = "telephone"),
            @Result(property = "givetime",column = "givetime"),
            @Result(property = "timelimit",column = "timelimit"),
            @Result(property = "tasktype",column = "tasktype"),
            @Result(property = "taskbackground",column = "taskbackground"),
            @Result(property = "taskmark",column = "taskmark"),
            @Result(property = "specification",column = "specification"),
            @Result(property = "keywords",column = "keywords"),
            @Result(property = "taskfeedback",column = "taskfeedback"),
            @Result(property = "kqfwPerformance",column = "id",javaType = List.class,many = @Many(select = "com.haina.vote.mapper.KqFwMapper.findPerformanceById"))
    })
    public KqFwModel findTaskById(Integer id);

    @Select("select * from tb_kqfw_task_performance where taskId=#{taskId}")
    public List<KqFwPerformance> findPerformanceById(Integer id);

    //添加科情服务任务单
    @Insert("insert into tb_kqfw_task(number,organization,taskname,linkman,telephone,givetime,timelimit,tasktype,taskbackground,taskmark,specification,keywords,taskfeedback,status,code,taskInfo,create_time,update_time)values (#{number},#{organization},#{taskname},#{linkman},#{telephone},#{givetime},#{timelimit},#{tasktype},#{taskbackground},#{taskmark},#{specification},#{keywords},#{taskfeedback},#{status},#{code},#{taskInfo},#{create_time},#{update_time})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    public void addTask(KqFwModel kqFwModel);

    //添加子表的任务信息
    @Insert("insert into tb_kqfw_task_performance(person_in_charge,level,task_allocation,taskId)values(#{person_in_charge},#{level},#{task_allocation},#{taskId})")
    public int addPerformance(KqFwPerformance kqFwPerformance);

    //删除任务单主表的信息
    @Delete("delete from tb_kqfw_task where id=#{id}")
    public int deleteTask(Integer id);

    //根据taskId 删除副表的相关所有项
    @Delete("delete from tb_kqfw_task_performance where taskId=#{taskId}")
    public void deletePerformance(Integer taskId);

    //根据id修改数据
    @Update("update tb_kqfw_task set number=#{number},taskname=#{taskname},organization=#{organization},linkman=#{linkman},telephone=#{telephone},givetime=#{givetime},timelimit=#{timelimit},tasktype=#{tasktype},taskbackground=#{taskbackground},taskmark=#{taskmark},specification=#{specification},keywords=#{keywords},taskfeedback=#{taskfeedback},taskInfo=#{taskInfo},means_url=#{means_url},update_time=#{update_time},status=#{status} where id=#{id}")
    public void updateTask(KqFwModel kqFwModel);

    //修改任务完成情况表
    @Update("update tb_kqfw_task_performance set person_in_charge=#{person_in_charge},level=#{level},task_allocation=#{task_allocation} where id=#{id}")
    public int updatePerformance(KqFwPerformance kqFwPerformance);

    //查找最新一条数据
    @Select("select * from tb_kqfw_task ORDER BY number DESC limit 1")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "number",column = "number"),
            @Result(property = "organization",column = "organization"),
            @Result(property = "taskname",column = "taskname"),
            @Result(property = "linkman",column = "linkman"),
            @Result(property = "telephone",column = "telephone"),
            @Result(property = "givetime",column = "givetime"),
            @Result(property = "timelimit",column = "timelimit"),
            @Result(property = "tasktype",column = "tasktype"),
            @Result(property = "taskbackground",column = "taskbackground"),
            @Result(property = "taskmark",column = "taskmark"),
            @Result(property = "specification",column = "specification"),
            @Result(property = "keywords",column = "keywords"),
            @Result(property = "taskfeedback",column = "taskfeedback"),
            @Result(property = "kqfwPerformance",column = "id",javaType = List.class,many = @Many(select = "com.haina.vote.mapper.KqFwMapper.findPerformanceById"))
    })
    public KqFwModel findTaskMaxTime();

    @Update("update tb_kqfw_task set status=#{status} where id=#{id}")
    public int setStatus(@Param("id")Integer id,@Param("status")Integer status);

    //驳回处理
    @Update("update tb_kqfw_task set status=#{reject_taskStatus},reject=#{reject_Info} where id=#{reject_taskId}")
    public int reject(@Param("reject_taskId")Integer reject_taskId,@Param("reject_taskStatus")Integer reject_taskStatus,@Param("reject_Info")String reject_Info);

    @Update("update tb_kqfw_task set means_url=#{means_url},status=#{status} where id=#{id}")
    public int updateMeansUrl(KqFwModel param);

    @Select("select * from tb_kqfw_task where id=#{id}")
    public KqFwModel downloadMeans(Integer id);

    @Update("update tb_kqfw_task set taskfeedback=#{taskfeedback},update_time=#{update_time} where id=#{id}")
    public int updatefeedback(KqFwModel kqFwModel);

    @Select("select * from tb_kqfw_task where taskname like concat('%',#{tl},'%')")
    public List<KqFwModel> findTaskByName(@Param("tl") String tl);
    //根据子表id 删除子表的项
    @Delete("delete from tb_kqfw_task_performance where id=#{id}")
    public int deletePerformanceById(Integer id);
    @Select("select 1 from tb_kqfw_task_performance where id=#{id}")
    public int findIsExist(Integer id);
    //根据子表自己的id进行查找
    @Select("select * from tb_kqfw_task_performance where id=#{pid}")
    public int findPerformanceByPid(Integer pid);
}
